﻿#include "guild_brief_logic.h"

#include "AnnounceMsg_struct.h"
#include "AnnounceData_struct.h"
#include "GuildModuleBrief_struct.h"

#include "error/make_error_code.h"
#include "fwd.h"
#include "guild/guild_loader.hpp"
#include "guild/module/guild_module_loader.hpp"

namespace guild_service {

bool GuildBriefLogic::GetGuildModuleBrief(const guild_id_type guild_id,
                                    GuildModuleBrief &brief) {
  return detail::GetGuildModule<GuildModuleBrief>(kGuildServiceQueryName, guild_id,
                                            kGuildModuleBriefName, brief);
}

bool GuildBriefLogic::GetGuildModuleBriefByRole(const role_uuid_type role_uuid,
                                    GuildModuleBrief &brief, error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceQueryName, role_uuid, guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  if (!GuildBriefLogic::GetGuildModuleBrief(guild_id, brief)) {
    ec = ErrorCode::ERR_GUILD_BRIEF_LOAD_FAILED;
    return false;
  }

  return true;
}

bool GuildBriefLogic::GetGuildIdByRole(const role_uuid_type role_uuid,
                                       guild_id_type &guild_id) {
  return detail::LoadGuildIdByRole(kGuildServiceQueryName, role_uuid, guild_id);
}

bool GuildBriefLogic::GetGuildBriefList(const role_uuid_type role_uuid,
                                        std::size_t count,
                                        std::vector<GuildModuleBrief> &brief_list,
                                        error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_ROLE_UUID;
    return false;
  }

  constexpr std::size_t max_count = 10;
  if (count == 0 || count > max_count) {
    count = max_count;
  }

  GuildBriefLogic::ForeachGuildBrief(
      [count, &brief_list](const GuildModuleBrief &tmp_brief) -> bool {
        brief_list.emplace_back(tmp_brief);
        return brief_list.size() < count;
      });

  return true;
}

bool GuildBriefLogic::SearchGuild(const role_uuid_type role_uuid,
                                  const std::string &name, GuildModuleBrief &brief,
                                  error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    return false;
  }

  GuildBriefLogic::ForeachGuildBrief(
      [&name, &brief](const GuildModuleBrief &tmp_brief) -> bool {
        if (tmp_brief.name != name) {
          return true;
        }
        brief = tmp_brief;
        return false;
      });

  return brief.name == name;
}

void GuildBriefLogic::ForeachGuildBrief(
    const ForeachGuildBriefCallbackType &callback) {
  if (!callback) {
    return;
  }

  std::vector<guild_id_type> guild_id_list;
  if (!detail::LoadAllGuildId(kGuildServiceQueryName, guild_id_list)) {
    return;
  }

  GuildModuleBrief tmp_brief;
  for (const auto &it : guild_id_list) {
    if (!GuildBriefLogic::GetGuildModuleBrief(it, tmp_brief)) {
      continue;
    }
    if (!callback(tmp_brief)) {
      break;
    }
  }
}
}  // namespace guild_service